package Simulator;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class CarThread extends JFrame
{ 

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public CarThread()
	{  
		setSize(1000, 200);
		setLocationRelativeTo(rootPane);
		setTitle("Traffic Visual");

		addWindowListener(new WindowAdapter(){ 
			public void windowClosing(WindowEvent e){ 
				// System.exit(0);
			}
		} 
		);

		Container contentPane = getContentPane();
		canvas = new JPanel();
		contentPane.add(canvas, "Center");
		JPanel p = new JPanel();
		
		addButton(p, "Start", new ActionListener(){  
			
			public void actionPerformed(ActionEvent evt){  
				Ball b = new Ball(canvas);
				b.start();
			}
		}
		);

		addButton(p, "Close", new ActionListener(){  
			public void actionPerformed(ActionEvent evt){  
				canvas.setVisible(false);
				//System.exit(0);
			}
		}
		);
		
		contentPane.add(p, "South");
	}

	public void addButton(Container c, String title, ActionListener a){  
		JButton b = new JButton(title);
		c.add(b);
		b.addActionListener(a);
	}

	private JPanel canvas;
}

class Ball extends Thread{  
	
	public Ball(JPanel b){ 
	box = b; 
	}

	public void draw()
	{  Graphics g = box.getGraphics();
		g.fillOval(x, y, XSIZE, YSIZE);
		g.dispose();
	}

	public void move(){  
		
		if (!box.isVisible()){ 
			return;
		}
		
		Graphics g = box.getGraphics();
		g.setXORMode(box.getBackground());
		g.fillOval(x, y, XSIZE, YSIZE);
		x += dx;
		y += dy;
		Dimension d = box.getSize();

		if (x < 0){ 
			x = 0; 
			dx = -dx; 
		}

		y = d.height - d.height/2; 
		
		g.fillOval(x, y, XSIZE, YSIZE);

		g.dispose();
}

	public void run(){ 
		
		try{  
			draw();
	
			for(int i = 1; i <= 1000; i++){  
				move();
				sleep(5);
			}
		}
		catch(InterruptedException e){
			
		}
	}

	private JPanel box;
	private static final int XSIZE = 10;
	private static final int YSIZE = 10;
	private int x = 0;
	private int y = 0;
	private int dx = 2;
	private int dy = 2;
}


